//
//  RateView.m
//  ebook
//
//  Created by 吴超飞 on 16/5/8.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "RateView.h"

@implementation RateView

-(instancetype)init{
    
    float width ;//= MTScreenW*29.0/32;
    float x;
    
    if (isIpad) {
        width = MTScreenW/3*2;
        x = width*0.2;
    } else {
        width = MTScreenW*29.0/32;
        x = MTScreenW*6.0/32;
    }
//    float x = MTScreenW*6.0/32;
    float height = width*556.0/579;

    CGRect frame = CGRectMake(0, 0, width, height);
    self = [super initWithFrame:frame];
    
    if (self) {
        
        UIImageView *bg = [[UIImageView alloc] initWithFrame:self.frame];
        [bg setImage:[UIImage imageNamed:@"rate_bg"]];
        [bg setContentMode:UIViewContentModeScaleToFill];
        [self addSubview:bg];
        
        float btnWidth ;//= width-2*x;
    
        if (isIpad) {
            btnWidth = width*0.6;
        } else {
            btnWidth = width-2*x;
        }
        
        float btnHeight = btnWidth*71.0/319;
        
        UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(x, width*120.0/276, btnWidth, btnHeight)];
        btn1.tag = 1;
        [btn1 setBackgroundImage:[UIImage imageNamed:@"rate_btn1_n"] forState:UIControlStateNormal];
        [btn1 setBackgroundImage:[UIImage imageNamed:@"rate_btn1_p"] forState:UIControlStateHighlighted];
        [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn1];
        
        UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(x, CGRectGetMaxY(btn1.frame)+10, btnWidth, btnHeight)];
        btn2.tag = 2;
        [btn2 setBackgroundImage:[UIImage imageNamed:@"rate_btn2_n"] forState:UIControlStateNormal];
        [btn2 setBackgroundImage:[UIImage imageNamed:@"rate_btn2_p"] forState:UIControlStateHighlighted];
        [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn2];
        
        UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(x, CGRectGetMaxY(btn2.frame)+10, btnWidth, btnHeight)];
        btn3.tag = 3;
        [btn3 setBackgroundImage:[UIImage imageNamed:@"rate_btn3_n"] forState:UIControlStateNormal];
        [btn3 setBackgroundImage:[UIImage imageNamed:@"rate_btn3_p"] forState:UIControlStateHighlighted];
        [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn3];
    }
    
    return self;
}

-(void)btnClick:(UIButton *)btn{
    
    if ([self.delegate respondsToSelector:@selector(rateView:rateType:)]) {
        [self.delegate rateView:self rateType:(RateType)(btn.tag-1)];
    }
}

@end
